﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.SoldierArchives.BLL;

public partial class BasicBase_SoldierArchive_NoUseCardIds : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        string Contract = Request["IsContract"];
        bool IsContract = Convert.ToBoolean(Convert.ToInt32(Contract));
        string Departmentid = Request["Departmentid"];
        string where = Request["where"];
        if (IsContract)
        { //合同制  
            userDL.Caption = "<span>合同制编号未使用</span>";
            //编码规则
            CardCodeInfo cardcode = CardCode.GetCardCodeInfoByDepartmentID(Departmentid);
            string FrontCode = cardcode.EmFrontCode;
            string StartNum = cardcode.EmStartNum;
            string EndNum = cardcode.EmEndNum;

            //可用士兵证号
            Soldier soldier = new Soldier();
            int count = soldier.GetSoldiersCountByWhere(where);
            List<SoldierArchiveInfo> useCardSoldiers = soldier.GetSoldiersByWhere(where, "DepartmentID", "ASC", count, 0);
            List<string> useCards = new List<string>();
            for (int i = 0; i < useCardSoldiers.Count(); i++)
            {
                useCards.Add(useCardSoldiers[i].CardID);
            }

            List<string> noUseCards = new List<string>();
            for (int i = Convert.ToInt32(StartNum); i <= Convert.ToInt32(EndNum); i++)
            {
                string Cardid = "";
                int l = 4 - i.ToString().Length;
                for (int j = 0; j < l; j++)
                {
                    Cardid += "0";
                }

                Cardid = FrontCode + Cardid + i.ToString();

                if (!useCards.Contains(Cardid))
                {
                    noUseCards.Add(Cardid);
                }
            }
            userDL.DataSource = noUseCards;
            userDL.DataBind();

            //编码规则

            lblEstablishment.Text = cardcode.ContractEstablishment.ToString();
            lblFrontCode.Text = cardcode.EmFrontCode;
            StartNum1.Text = cardcode.EmStartNum;
            EndNum1.Text = cardcode.EmEndNum;
            NoUseCount.Text = noUseCards.Count.ToString();
        }
        else
        {
            //现役士兵
            //编码规则
            CardCodeInfo cardcode = CardCode.GetCardCodeInfoByDepartmentID(Departmentid);
            string FrontCode = cardcode.FrontCode;
            string StartNum = cardcode.StartNum;
            string EndNum = cardcode.EndNum;

            //可用士兵证号
            Soldier soldier = new Soldier();
            int count = soldier.GetSoldiersCountByWhere(where);
            List<SoldierArchiveInfo> useCardSoldiers = soldier.GetSoldiersByWhere(where, "DepartmentID", "ASC", count, 0);
            List<string> useCards = new List<string>();
            for (int i = 0; i < useCardSoldiers.Count(); i++)
            {
                useCards.Add(useCardSoldiers[i].CardID);
            }

            List<string> noUseCards = new List<string>();
            for (int i = Convert.ToInt32(StartNum); i <= Convert.ToInt32(EndNum); i++)
            {
                string Cardid = "";
                int l = 4 - i.ToString().Length;
                for (int j = 0; j < l; j++)
                {
                    Cardid += "0";
                }

                Cardid = FrontCode + Cardid + i.ToString();

                if (!useCards.Contains(Cardid))
                {
                    noUseCards.Add(Cardid);
                }
            }
            userDL.DataSource = noUseCards;
            userDL.DataBind();

            //编码规则

            lblEstablishment.Text = cardcode.Establishment.ToString();
            lblFrontCode.Text = cardcode.FrontCode;
            StartNum1.Text = cardcode.StartNum;
            EndNum1.Text = cardcode.EndNum;
            NoUseCount.Text = noUseCards.Count.ToString();
        }
    }
    protected void userDL_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Label lblCard = e.Item.FindControl("lblCard") as Label;
            lblCard.Text = e.Item.DataItem.ToString();
        }
    }
}
